package com.wanhe.vip.module.info;

import com.google.gson.JsonObject;
import com.orhanobut.logger.Logger;
import com.wanhe.vip.api.RetrofitService;
import com.wanhe.vip.api.bean.CardInfo;
import com.wanhe.vip.api.bean.PageResponse;
import com.wanhe.vip.api.bean.VipCard;
import com.wanhe.vip.api.bean.VipCoupon;
import com.wanhe.vip.module.base.IBasePresenter;
import com.wanhe.vip.widget.EmptyLayout;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Created by XieZaiWei on 2017/4/18.
 */

public class VipInfoPresenter implements IBasePresenter<IVipView> {

	private IVipView mVipView;

	public VipInfoPresenter() {
	}

	@Override
	public void setView(IVipView view) {
		mVipView = view;
	}

	/**
	 * 获得卡列表
	 *
	 * @param phone
	 */
	public void getVipCardList(String phone) {
		RetrofitService.getVipCardList(phone)
				.doOnSubscribe(new Consumer<Disposable>() {
					@Override
					public void accept(@NonNull Disposable disposable) {
						mVipView.showLoading();
					}
				})
				.subscribe(new Observer<PageResponse<VipCard>>() {
					@Override
					public void onSubscribe(@NonNull Disposable d) {

					}

					@Override
					public void onComplete() {
						mVipView.hideLoading();
					}

					@Override
					public void onError(Throwable e) {
						Logger.e(e.toString());
						mVipView.showNetError(new EmptyLayout.OnRetryListener() {
							@Override
							public void onRetry() {
							}
						});
					}

					@Override
					public void onNext(PageResponse<VipCard> pageResponse) {
						mVipView.showCardList(pageResponse.list);
					}
				});
	}

	/**
	 * 获得单张卡信息
	 *
	 * @param cardNumber
	 * @param birthday
	 */
	public void getCardInfo(String cardNumber, final String birthday) {
		RetrofitService.getCardInfo(cardNumber)
				.doOnSubscribe(new Consumer<Disposable>() {
					@Override
					public void accept(@NonNull Disposable disposable) {
						mVipView.showLoading();
					}
				})
				.subscribe(new Observer<CardInfo>() {
					@Override
					public void onSubscribe(@NonNull Disposable d) {

					}

					@Override
					public void onComplete() {
						mVipView.hideLoading();
					}

					@Override
					public void onError(Throwable e) {
						Logger.e(e.toString());
						mVipView.showNetError(new EmptyLayout.OnRetryListener() {
							@Override
							public void onRetry() {
							}
						});
					}

					@Override
					public void onNext(CardInfo cardInfo) {
						cardInfo.setBirthday(birthday);
						mVipView.showCardInfo(cardInfo);
					}
				});
	}

	public void preConsume(String cardId, String billAmount, String prePay, String cash, VipCoupon vipCouponModel) {

		RetrofitService.preConsume(cardId, billAmount, prePay, cash, vipCouponModel)
				.doOnSubscribe(new Consumer<Disposable>() {
					@Override
					public void accept(@NonNull Disposable disposable) {
						mVipView.showLoading();
					}
				})
				.subscribe(new Observer<JsonObject>() {
					@Override
					public void onSubscribe(@NonNull Disposable d) {

					}

					@Override
					public void onComplete() {
						mVipView.hideLoading();
					}

					@Override
					public void onError(Throwable e) {
						Logger.e(e.toString());
						mVipView.showNetError(new EmptyLayout.OnRetryListener() {
							@Override
							public void onRetry() {
							}
						});
					}

					@Override
					public void onNext(JsonObject result) {
						mVipView.preConsumeSucess();
					}
				});
	}

	public void doConsume(String cardId, String billAmount, String prePay, String cash, VipCoupon vipCouponModel, String verifyCode) {

		RetrofitService.preConsume(cardId, billAmount, prePay, cash, vipCouponModel)
				.doOnSubscribe(new Consumer<Disposable>() {
					@Override
					public void accept(@NonNull Disposable disposable) {
						mVipView.showLoading();
					}
				})
				.subscribe(new Observer<JsonObject>() {
					@Override
					public void onSubscribe(@NonNull Disposable d) {

					}

					@Override
					public void onComplete() {
						mVipView.hideLoading();
					}

					@Override
					public void onError(Throwable e) {
						Logger.e(e.toString());
						mVipView.showNetError(new EmptyLayout.OnRetryListener() {
							@Override
							public void onRetry() {
							}
						});
					}

					@Override
					public void onNext(JsonObject result) {
						mVipView.preConsumeSucess();
					}
				});
	}

}
